package com.ZYHsheep.dao;

import com.ZYHsheep.entity.Fixhoof;
import com.ZYHsheep.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FixhoofDao {
    /*新增修蹄 */
    public int insert(Fixhoof f) {
        String sql = "INSERT INTO fixhoof(Sno, technician, event_time) VALUES (?,?,?)";
        try (Connection conn = JDBCUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, f.getSno());
            ps.setString(2, f.getTechnician());
            ps.setTimestamp(3, Timestamp.valueOf(f.getEventTime()));
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /*修蹄记录 */
    public List<Fixhoof> findAll() {
        List<Fixhoof> list = new ArrayList<>();
        String sql = "SELECT * FROM fixhoof ORDER BY event_time DESC";
        try (Connection conn = JDBCUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Fixhoof f = new Fixhoof();
                f.setId(rs.getInt("id"));
                f.setSno(rs.getInt("Sno"));
                f.setTechnician(rs.getString("technician"));
                Timestamp ts = rs.getTimestamp("event_time");
                f.setEventTime(ts != null ? ts.toLocalDateTime() : null);
                list.add(f);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
